package com.znsb.msfq.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.znsb.msfq.R;
import com.znsb.msfq.album.ImageFileActivity;
import com.znsb.msfq.bean.ShowBillBean;
import com.znsb.msfq.utils.DisplayUtil;
import com.znsb.msfq.utils.GetIsparityTime;
import com.znsb.msfq.utils.OnItemClickListener;
import com.znsb.msfq.utils.SPUtils;
import com.znsb.msfq.utils.ValidateUtils;
import com.znsb.msfq.view.NoScrollGridView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * @name：
 * @author： 孙科技
 * @phone： 13308334117
 * @createTime： 2017/2/10
 * @modifyTime： 2017/2/10
 * @explain：
 */

public class MyShowBillRVAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ShowBillBean.DataListBean> dataList;
    private Context context;


    public MyShowBillRVAdapter(Context context, List<ShowBillBean.DataListBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    public void updateList(List<ShowBillBean.DataListBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
        notifyItemRemoved(getItemCount());
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(context).inflate(R.layout.item_showbill_view, parent, false);
        return new MyHolder(layout);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MyHolder) {
            if (ValidateUtils.isNullOrEmpty(dataList.get(position).getUserName())) {
                ((MyHolder) holder).showbillTvName.setText(ValidateUtils.getShowHint(SPUtils.getPhone() + "", 3, 7));
            } else
                ((MyHolder) holder).showbillTvName.setText(ValidateUtils.getShowHint(dataList.get(position).getUserName() + "", 3, 7));
            ((MyHolder) holder).showbillTvTime.setText(GetIsparityTime.InputTimetampAll(dataList.get(position).getCreateTime() + ""));
            ((MyHolder) holder).showbillTvAppraise.setText(dataList.get(position).getInformation() + "");
            if (dataList.get(position).getImages().size() == 4)
                ((MyHolder) holder).showbillGridView.setNumColumns(2);
            else {
                ((MyHolder) holder).showbillGridView.setNumColumns(3);
            }
            ((MyHolder) holder).showbillGridView.setAdapter(new BrowerAdapter(context, dataList.get(position).getImages(), "320,480"));
//            Picasso.with(context).load(dataList.get(position).getLogo() + "").placeholder(R.mipmap.ic_launcher).into(((MyHolder) holder).collectImgHead);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView, position);
                }
            });
        }
    }

    class MyHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.showbill_img_head)
        ImageView showbillImgHead;
        @Bind(R.id.showbill_tv_name)
        TextView showbillTvName;
        @Bind(R.id.showbill_tv_time)
        TextView showbillTvTime;
        @Bind(R.id.showbill_tv_appraise)
        TextView showbillTvAppraise;
        @Bind(R.id.showbill_grid_view)
        NoScrollGridView showbillGridView;

        public MyHolder(View view) {
            super(view);
//            R.layout.item_showbill_view
            ButterKnife.bind(this, view);
        }
    }
}
